<?php

class DataLayer {
	private $useTable;
	private $db;
	private $useConnection;
	private $debug;
	
	public function __construct($useTable, $useConnection) {
		if ($useConnection == true) {
			$this->useTable = $useTable;
			$this->useConnection = true;
			$this->db = new DBConnection();
		} else {
			$this->useTable = null;
			$this->useConnection = false;
		}
		
		$this->debug = Configure::read('debug');
	}
	
	public function query ($sql) {
		if ($this->useConnection == false) {
			throw new CherryException("Cannot use query method, check your useConnection property in your class", 400);
		}
		
		if ($this->debug == 1) {
			Debug::write($sql);
		}
		
		return $this->db->query($sql);
	}
	
	public function execute($sql) {
		if ($this->useConnection == false) {
			throw new CherryException("Cannot use execute method, check your useConnection property in your class", 400);
		}
		
		if ($this->debug == 1) {
			Debug::write($sql);
		}
		
		return $this->db->execute($sql);
	}
	
	public function getAll($conditions = array()) {
		if ($this->useConnection == false) {
			throw new CherryException("Cannot use getAll method, check your useConnection property in your class", 400);
		}
		
		if (isset($conditions['fields'])) {
			if (is_array($conditions['fields'])) {
				$fields = implode(", ", $conditions['fields']);
			} else {
				if (trim($conditions['fields']) != "") {
					$fields = $conditions['fields'];
				} else {
					$fields = "*";
				}
			}
		} else {
			$fields = "*";
		}
		
		if (isset($conditions['where'])) {
			if (is_array($conditions['where'])) {
				$where = " WHERE ". implode("AND ", $conditions['where']);
			} else {
				if (trim($conditions['where']) != "") {
					$where = " WHERE ". $conditions['where'];
				}
			}
		}
		
		if (isset($conditions['group'])) {
			if (is_array($conditions['group'])) {
				$group = " GROUP BY ". implode(", ", $conditions['group']);
			} else {
				if (trim($conditions['group']) != "") {
					$group = " GROUP BY ". $conditions['group'];
				}
			}
		}
		
		if (isset($conditions['having'])) {
			if (is_array($conditions['having'])) {
				$having = " HAVING ". implode("AND ", $conditions['having']);
			} else {
				if (trim($conditions['having']) != "") {
					$having = " HAVING ". $conditions['having'];
				}
			}
		}
		
		if (isset($conditions['order'])) {
			if (is_array($conditions['order'])) {
				$order = " ORDER BY ". implode(", ", $conditions['order']);
			} else {
				if (trim($conditions['order']) != "") {	
					$order = " ORDER BY ". $conditions['order'];
				}
			}
		}
		
		if (isset($conditions['limit'])) {
			if (is_array($conditions['limit'])) {
				$limit = " LIMIT ". implode(", ", $conditions['limit']);
			} else {
				if (trim($conditions['limit']) != "") {	
					$limit = " LIMIT ". $conditions['limit'];
				}
			}
		}
		
		$sql = "SELECT ". $fields ." FROM ". $this->useTable . $where . $group . $having . $order . $limit;
		
		if ($this->debug == 1) {
			Debug::write($sql);
		}
		
		return $this->db->query($sql);
	}
	
	public function getCount($conditions = array()) {
		$conditions['fields'] = "COUNT(*) as count";
		return $this->getAll($conditions);;
	}
	
	public function getFirst($conditions = array()) {
		$data = $this->getAll($conditions);
		return array_pop($data);
	}
	
	public function find($type, $conditions = array()) {
		if ($this->useConnection == false) {
			throw new CherryException("Cannot use find method, check your useConnection property in your class", 400);
		}
		
		$type = trim($type);
		
		if ($type == 'all') {
			return $this->getAll($conditions);
		} else if ($type == 'first') {
			return $this->getFirst($conditions);
		} else if ($type == 'count') {
			return $this->getCount($conditions);
		} else {
			throw new CherryException("Unknown find method ".$type, 400);
		}
	}
}

?>